# 3.36 WiFi Control ## 3.36.1 Overview We control LED, servo and the fan on the web page button wirelessly. ## 3.36.2 Test Code In Files, open **3-36-wifi-WebpageControl.py** and click ![](media/run.jpg). **Code:** ```python ''' * Filename : 3-36-wifi-WebpageControl * Thonny : Thonny 4.1.4 * Auther : http//www.keyestudio.com ''' import network import socket import time import machine from machine import Pin, PWM from servo import Servo # WiFi connection information SSID = 'test1' # your WiFi name PASSWORD = '88888888' # your WiFi password # initialize executer ledRed = Pin(23, Pin.OUT) ledYellow = Pin(26, Pin.OUT) ledGreen = Pin(27, Pin.OUT) servo = Servo(pin=25) MA = Pin(18, Pin.OUT) MB = Pin(17, Pin.OUT) # connect to WiFi def connect_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) timeout = 10 while not wlan.isconnected() and timeout > 0: print("Connecting to WiFi...") time.sleep(1) timeout -= 1 if not wlan.isconnected(): raise Exception("Could not connect to WiFi") print('Network config:', wlan.ifconfig()) print('Connected to WiFi, IP address:', wlan.ifconfig()[0]) return wlan # create HTML page def web_page(): html = """ ESP32 Control

ESP32 Control





""" return html # Control actuator def handle_request(request): if 'ledRed_on' in request: ledRed.value(1) elif 'ledRed_off' in request: ledRed.value(0) elif 'ledYellow_on' in request: ledYellow.value(1) elif 'ledYellow_off' in request: ledYellow.value(0) elif 'ledGreen_on' in request: ledGreen.value(1) elif 'ledGreen_off' in request: ledGreen.value(0) elif 'servo_left' in request: servo.set_angle(0) # rotate to left elif 'servo_right' in request: servo.set_angle(180) # rotate to right elif 'fan_on' in request: MA.value(1) MB.value(0) elif 'fan_off' in request: MA.value(0) MB.value(0) # Start the Web server def start_server(): wlan = connect_wifi(SSID, PASSWORD) addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(5) print('Listening on', addr) while True: cl, addr = s.accept() print('Client connected from', addr) request = cl.recv(1024) request = str(request) print('Content = %s' % request) handle_request(request) response = web_page() cl.send('HTTP/1.1 200 OK\n') cl.send('Content-Type: text/html\n') cl.send('Connection: close\n\n') cl.sendall(response) cl.close() # run server try: start_server() except Exception as e: print('Failed to start server:', e) machine.reset() ``` **Result:** Upload the code, and the Shell shows the IP address after connecting to wifi. Connect your computer/mobile phone and ESP32 to the same wifi, and you can access the IP address to see the control page through your device. ![](./media/7-36-1.png)